﻿using System;
using System.Collections.Generic;
using System.Linq;
using EduFormManager.Models;
using EduFormManager.Models.Repo;
using System.Collections.Generic;

namespace EduFormManager.Forms.UserControls.MunicipalityFormulaPeek
{
    public partial class MunicipalityFormulaPeekControl : DevExpress.XtraEditors.XtraUserControl
    {
        private Municipality _municipality;
        private Allform _munitForm;
        private Allform _eduForm;
        private int _year;
        private MunicipalitySummaryFormula _formula;

        public event EventHandler<MunicipalityFormulaPeekArgs> Completed;

        public MunicipalityFormulaPeekControl(Municipality munit, Allform munitForm, int year)
        {
            InitializeComponent();
            IsAvailable = false;

            Initialize(munit, munitForm, year);
        }

        async private void Initialize(Municipality munit, Allform munitForm, int year)
        {
            _formula = munitForm.MunicipalitySummaryFormula;
            this.IsAvailable = _formula != null;
            if (this.IsAvailable)
            {
                _municipality = munit;
                _munitForm = munitForm;
                _eduForm = _formula.EduForm;
                _year = year;

                IList<EduOrg> eduOrgList = await RepoHelper.Repo.GetEduOrgsHaveFormDataAsync(_eduForm, _year);
                this.listBoxControlEdu.DataSource = eduOrgList.Where(t => t.Municipality.Equals(munit)).ToList();
            }

            this.buttonCancel.Click += CompleteHandler;
            this.buttonOk.Click += CompleteHandler;
            this.checkEditCheckAll.CheckedChanged += (sender, args) =>
            {
                if (this.checkEditCheckAll.Checked)
                    this.listBoxControlEdu.CheckAll();
                else
                    this.listBoxControlEdu.UnCheckAll();
            };
        }

        public bool IsAvailable { get; private set; }

        private void CompleteHandler(object sender, EventArgs e)
        {
            MunicipalityFormulaPeekArgs args = new MunicipalityFormulaPeekArgs();
            args.Write = this.IsAvailable;
            if (IsAvailable)
            {
                foreach (int index in this.listBoxControlEdu.CheckedIndices)
                {
                    EduOrg edu = (EduOrg) this.listBoxControlEdu.GetItem(index);
                    FormData data = edu.FormData.FirstOrDefault(t => t.Form == _eduForm && t.Date.Year == _year);
                    if (data == null) continue;
                    args.FormDataList.Add(data);
                }
                args.FormulaBytes = _formula.FormulaBytes;
            }
            OnCompleted(args);
        }

        private void listBoxControlEdu_GetItemEnabled(object sender, DevExpress.XtraEditors.Controls.GetItemEnabledEventArgs e)
        {
            /*EduOrg edu = this.listBoxControlEdu.GetItem(e.Index) as EduOrg;
            if (edu == null) return;

            e.Enabled = edu.FormData.Any(t => t.Form == _eduForm && t.Date.Year == _year);*/
        }

        protected virtual void OnCompleted(MunicipalityFormulaPeekArgs e)
        {
            var handler = Completed;
            if (handler != null) handler(this, e);
        }
    }
}
